package com.bhcj.telling.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebChromeClient;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bhcj.telling.R;
import com.bhcj.telling.view.activity.ExplainActivity;
import com.bhcj.telling.utils.ConstantUtil;

public class GuaTuListFragment extends Fragment {

    private View mRootView;
    private WebView webView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_gua_tu_list, container, false);
        webView = mRootView.findViewById(R.id.webview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true); // 启用JavaScript，如果需要的话
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/html/gua.html");
        webView.addJavascriptInterface(new WebAppInterface(), "gua");
        return mRootView;
    }

    public class WebAppInterface {
        public WebAppInterface() {

        }
        @JavascriptInterface
        public void myMethod(String key) {
            // 在这里执行你的操作或调用方法
            Intent intent = new Intent(getActivity(), ExplainActivity.class);
            intent.putExtra(ConstantUtil.GUA_IDENTIFY, key);
            startActivity(intent);
        }
    }


    public static GuaTuListFragment newInstance(Bundle bundle) {
        GuaTuListFragment fragment = new GuaTuListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
}